package com.crdeng.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crdeng.handler.BusinessException;
import com.crdeng.response.Result;
import com.crdeng.response.ResultCode;
import com.crdeng.system.entity.Role;
import com.crdeng.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author crdeng
 * @since 2020-11-27
 */

@Api(value = "角色模块", tags = "角色功能模块")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "角色列表", notes = "查询角色列表")
    @PostMapping("/getRoleList")
    public Result getRoleList(@RequestParam(defaultValue = "1") Integer current,
                              @RequestParam(defaultValue = "6") Integer size,
                              @RequestParam(required = false) String roleName) {
        Page<Role> rolePage = new Page<>(current, size);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)) {
            System.out.println(roleName);
            queryWrapper.like("role_name", roleName);
        }
        Page<Role> page = roleService.page(rolePage, queryWrapper);
        if (page == null) {
            throw new BusinessException(ResultCode.ROLE_NOT_EXIST.getCode(), ResultCode.ROLE_NOT_EXIST.getMessage());
        }

        return Result.ok().data("roleList", page);

    }

    @ApiOperation(value = "删除角色", notes = "根据ID删除角色")
    @GetMapping("/deleteRole")
    public Result deleteRole(@RequestParam Long id) {

        boolean flag = roleService.removeById(id);
        if (flag) {
            return Result.ok().message("删除角色成功");
        } else {
            throw new BusinessException(ResultCode.ROLE_NOT_EXIST.getCode(), ResultCode.ROLE_NOT_EXIST.getMessage());
        }
    }

    @ApiOperation(value = "修改角色", notes = "根据ID修改角色")
    @PostMapping("/amendRole")
    public Result amendRole(@RequestBody Role role) {
        System.out.println(role);
        System.out.println(role);
        System.out.println(role);

        role.setModifiedTime(new Date());
        boolean flag = roleService.updateById(role);
        if (flag) {
            return Result.ok().message("修改角色成功");
        } else {
            throw new BusinessException(ResultCode.ROLE_NOT_EXIST.getCode(), ResultCode.ROLE_NOT_EXIST.getMessage());
        }
    }

    @ApiOperation(value = "添加角色", notes = "添加角色")
    @PostMapping("/addRole")
    public Result addRole(@RequestBody Role role) {
        role.setCreateTime(new Date());
        role.setModifiedTime(new Date());
        role.setStatus(1);
        boolean flag = roleService.save(role);
        if (flag) {
            return Result.ok().message("添加角色成功");
        } else {
            return  Result.error().message("添加角色失败");
        }
    }


    @ApiOperation(value = "修改角色状态", notes = "修改角色状态")
    @PostMapping("/updateRoleStatus")
    public Result updateRoleStatus(@RequestBody Role role) {
       if (role.getStatus()!=0&&role.getStatus()!=1){
           throw  new BusinessException(ResultCode.PARAM_NOT_VALID.getCode(),ResultCode.PARAM_NOT_VALID.getMessage());
       }
       role.setModifiedTime(new Date());
        boolean flag = roleService.updateById(role);
        if (flag) {
            return Result.ok().message("修改角色状态成功");
        } else {
            return  Result.error().message("修改角色状态失败");
        }
    }

    @ApiOperation(value = "给角色授权",notes = "给角色在中间表中授权")
    @PutMapping("/saveMenu_Role")
    public Result saveMenu_Role(@RequestParam Integer id,@RequestBody List<Integer> menuIds){
        roleService.saveMenu_Role(id,menuIds);
        return Result.ok();
    }


    @ApiOperation(value = "获取角色已经拥有的权限",notes = "获取角色已经拥有的权限")
    @GetMapping
            ("/getRole2Menu")
    public Result getRole2Menu(@RequestParam Integer id){
        List<Integer> menuId = roleService.getMenuId(id);
        return Result.ok().data("menuIdList",menuId);
    }

}

